<template>
  <Modal
      width="800"
      v-model="visible"
      :title="title"
      :key="key"
  >
    <div style="width: 80%;padding-left: 10%;padding-right: 10%">
      <Spin size="large" fix v-if="Loading"></Spin>
      <Form :label-width="130" :disabled="disableSubmit" ref="formValidate" :model="formValidate"
            :rules="ruleValidate">
        <Form-item label="动作名称:" prop="actionName">
          <Input v-model="formValidate.actionName" placeholder="请输入名称"/>
        </Form-item>
        <Form-item label="动作描述:" prop="actionDesc">
          <Input v-model="formValidate.actionDesc" placeholder="请输入描述"/>
        </Form-item>
        <Form-item label="实体名称:" prop="actionClass">
          <Select v-model="formValidate.actionClass" placeholder="请选择实体名称" @on-change="getRuleEntityItemList">
            <Option v-for="item in ruleEntityList" :value="item.pkgName" :key="item.id">{{ item.entityName }}
            </Option>
          </Select>
        </Form-item>
        <Form-item label="参数名" prop="parameterName">
          <Select v-model="formValidate.parameterName" placeholder="请选择参数名" @on-change="changeRuleEntityItem">
            <Option v-for="item in ruleEntityItem" :value="item.id" :key="item.id">{{ item.itemName }}
            </Option>
          </Select>
        </Form-item>
        <Form-item label="参数标识" prop="parameter">
          <Input v-model="formValidate.parameter" placeholder="请输入参数标识" disabled="disabled"/>
        </Form-item>
        <Form-item label="参数值" prop="parameterValue">
          <Input v-model="formValidate.parameterValue" placeholder="请输入参数值(例：$id$*5、value)"/>
        </Form-item>
        <Form-item label="备注">
          <Input v-model="formValidate.remarks" placeholder="请输入备注"/>
        </Form-item>
      </Form>
    </div>
    <div slot="footer">
      <Button @click="cancel">取消</Button>
      <template v-if="!disableSubmit">
        <Button type="primary" @click="ok">确定</Button>
      </template>
    </div>
  </Modal>
</template>

<script>
import {getAction, postAction} from '@/api/request'

export default {
  data() {
    return {
      modal1: false,
      title: "",
      visible: false,
      disableSubmit: false,
      Loading: false,
      formValidate: {},
      ruleEntityList: {},
      ruleEntityItem: {},
      key: "",
      ruleValidate: {
        actionName: [
          {required: true, message: '动作名称不能为空', trigger: 'blur'}
        ],
        actionDesc: [
          {required: true, message: '动作描述不能为空', trigger: 'blur'}
        ],
        actionClass: [
          {required: true, message: '实体名称不能为空', trigger: 'blur'}
        ],
        parameterName: [
          {required: true, message: '参数名不能为空', trigger: 'blur'}
        ],
        parameterValue: [
          {required: true, message: '参数标识不能为空', trigger: 'blur'}
        ],
        parameter: [
          {required: true, message: '参数值不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    ok() {
      var that = this;
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          that.submitFrom();
        }
      })
    },

    submitFrom() {
      var that = this;
      that.Loading = true;
      var url = "/api/ac/subsistence/RuleActionInforService/insert";
      if (this.formValidate.id) {
        url = "/api/ac/subsistence/RuleActionInforService/update"
      }
      postAction(url, this.formValidate).then((res) => {
        if (res.data.errcode != 0) {
          this.$Message.error(res.data.errmsg);
        } else {
          this.$emit('ok');
          this.visible = false;
        }

      }).finally(() => {
        that.Loading = false;
      });
    },
    cancel() {
      this.visible = false;
    },
    add() {
      this.edit({});
    },
    edit(row) {
      this.visible = true;
      this.formValidate = Object.assign({}, row);
      this.getRuleEntityList(row);

    },
    //获取实体列表
    getRuleEntityList(row) {
      var that = this;
      that.Loading = true;
      getAction("/api/ac/subsistence/RuleEntityService/list", {}).then((res) => {
        that.ruleEntityList = res.data.data;
        if (row.id) {
          this.getRuleEntityItemList(row.actionClass);
        }
      }).finally(() => {
        that.Loading = false;
      });
    },

    getRuleEntityItemList(key) {
      var that = this;
      that.Loading = true;
      var id = "";
      for (var i in this.ruleEntityList) {
        if (this.ruleEntityList[i].pkgName == key) {
          id = this.ruleEntityList[i].id;
          break;
        }
      }
      postAction("/api/ac/subsistence/RuleEntityService/itemlist", {ruleId: id}).then((res) => {
        this.ruleEntityItem = res.data.data;
      }).finally(() => {
        that.Loading = false;
      });
    },
    changeRuleEntityItem(key) {
      var id = "";
      for (var i in this.ruleEntityItem) {
        if (this.ruleEntityItem[i].id == key) {
          this.formValidate.parameterId = key;
          this.formValidate.parameter = this.ruleEntityItem[i].itemIdentify;
        }
      }
    },
  }
}
</script>

<style scoped>

</style>